package testing;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;

public class KentMapNeighbours {
	final double m = 0.7;
	static HashMap<Integer, Double> neighbourMap = new HashMap<Integer, Double>();
	
	public HashMap<Integer,Double> createMap(int numberOfSourceFrags) {
		/*
		 * Key = value due to some calculation Value= index to the source
		 * fragments because this hash map is used each time to extract degree
		 * largest values and use that index to point to a source fragment index
		 */
		HashMap<Integer, Double> Y = new HashMap<Integer, Double>();
		Random r = new Random();
		double nextValue,previousValue; 
		int firstKey = 0;
		double firstValue = r.nextDouble();
		Y.put(firstKey, firstValue);
		// need to swap the menaing of key and value and define the map properly 
		for ( int i=1;i<numberOfSourceFrags;i++){
			previousValue=Y.get(i-1);
			if(previousValue >0 && previousValue<=m){
				nextValue=(previousValue/m);
				Y.put(i,nextValue);
			}else {
				nextValue=Math.abs((1-previousValue)/1-m) ;
				Y.put(i, nextValue);
			}
			
		}
		return Y;

	}
	
	public ArrayList<Integer> returnSourceIndex(int degree){
		ArrayList<Integer> sourceIndex = new ArrayList<Integer>();
		  ValueComparator bvc =  new ValueComparator(neighbourMap);
		TreeMap<Integer,Double>   sortedMap = new TreeMap<Integer, Double>(bvc);
		sortedMap.putAll(neighbourMap);
		int i=0;
		 for (Integer key : sortedMap.keySet()) {
			 if(i<degree){
				 i++;
				sourceIndex.add(key);
	            System.out.println("key/value: " + key + "/"+sortedMap.get(key));
//	            neighbourMap.put(key, (double) 0);
			 }else {
				 break;
			 }
	        }
		return sourceIndex;
	}
	class ValueComparator implements Comparator {

		  Map<Integer,Double> base;
		  public ValueComparator(Map<Integer,Double> base) {
		      this.base = base;
		  }

		  public int compare(Object a, Object b) {

		    if((Double)base.get(a) < (Double)base.get(b)) {
		      return 1;
		    } else if((Double)base.get(a) == (Double)base.get(b)) {
		      return 0;
		    } else {
		      return -1;
		    }
		  }
		}
	public static void main ( String[] args){
		KentMapNeighbours k = new KentMapNeighbours();
		ArrayList<Integer> srcIdx;
		neighbourMap=k.createMap(10);
		Set<Integer> keys= neighbourMap.keySet();
		Iterator<Integer> iter=keys.iterator();
		while(iter.hasNext()){
			int currKey=iter.next();
			System.out.println(" kent map creation key"+currKey+" "+(double)neighbourMap.get(currKey));
		}
		srcIdx=k.returnSourceIndex(2);
		Iterator<Integer> iter2=srcIdx.iterator();
		while(iter2.hasNext()){
			int key=iter2.next();
			neighbourMap.put(key, 0.0);
		}
		for (Integer iter3 :neighbourMap.keySet()){			
			System.out.println("after null key"+iter3+" "+(double)neighbourMap.get(iter3));
		}
		srcIdx=k.returnSourceIndex(3);
	}

}
